import { defineStore } from 'pinia';
import { messageFn } from '../../utils';

import {
    getCostReturnList,//业务综合办理-费用退还-获取用水项目费用
    getlateFeeReturnList,//业务综合办理-违约金退还-获取违约金费用
    costReturn, //业务综合办理-费用退还
    lateFeeReturn //业务综合办理-违约金退还-获取违约金费用
} from '/@/api/comprehensive/feeRefund';

import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
    tip: '加载中...',
});


interface feeRefundFace {
    CostReturnList: []
    LateFeeList: [];
    total: number;
    pageInfo: object;
    storeBreachMoney: number; //减免违约金额
}

export const feeRefundStore = defineStore({
    id: 'feeRefund-state',
    state: (): feeRefundFace => ({
        CostReturnList: [],//获取用水项目费用
        LateFeeList: [], //获取违约金费用
        total: 0,//总页数
        pageInfo: {
            //分页
            page_no: 1,
            page_size: 10,
        },
        storeBreachMoney: 0//减免违约金额
    }),
    getters: {

    },
    actions: {
        setLateFeeList(list: []) {
            //获取到的列表数据
            this.LateFeeList = list ? list : [];
        },
        setCostReturnList(list: []) {
            //获取到的列表数据
            this.CostReturnList = list ? list : [];
        },
        setTotal(total: number) {
            console.log('setTotal----', total);
            //总页数
            this.total = total;
        },
        setPageInfo(pageParams: object) {
            //设置页面数据
            this.pageInfo = pageParams;
        },
        setStoreBreachMoney(num: number) {
            console.log('setStoreBreachMoney----', num);
            this.storeBreachMoney = num;
        },

        /**
         * @description: getCostReturnList //业务综合办理-费用退还-获取用水项目费用
         */
        async getCostReturnListAction(params: object) {
            //分页请求数据
            openFullLoading()
            try {
                const CostReturnList = await getCostReturnList(params);
                if (CostReturnList.code === 0) {
                    this.setLateFeeList(CostReturnList.data);
                    this.setTotal(CostReturnList.total);
                    return CostReturnList.data;
                } else {
                    messageFn('error', CostReturnList.msg);
                }
            } finally {
                closeFullLoading()
            }
        },

        /**
 * @description: getlateFeeReturnList //业务综合办理-违约金退还-获取违约金费用
 */
        async getlateFeeReturnListAction(params: object) {
            //分页请求数据
            openFullLoading()
            try {
                const lateFeeReturnList = await getlateFeeReturnList(params);
                if (lateFeeReturnList.code === 0) {
                    this.setLateFeeList(lateFeeReturnList.data);
                    this.setTotal(lateFeeReturnList.total);
                    return lateFeeReturnList.data;
                } else {
                    messageFn('error', lateFeeReturnList.msg);
                }
            } finally {
                closeFullLoading()
            }
        },

        /**
  * @description: //业务综合办理-违约金退还-获取违约金费用
  */
        async lateFeeReturnAction(params: object) {
            openFullLoading()
            try {
                const lateFee = await lateFeeReturn(params);
                if (lateFee.code === 0) {
                    messageFn('success', lateFee.msg);
                    // this.getLateFeeListAction(this.pageInfo);
                    return true;
                } else {
                    messageFn('error', lateFee.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },

        /**
* @description: costReturn //业务综合办理-费用退还
*/
        async costReturnAction(params: object) {
            openFullLoading()
            try {
                const costR = await costReturn(params);
                if (costR.code === 0) {
                    messageFn('success', costR.msg);
                    // this.getLateFeeListAction(this.pageInfo);
                    return true;
                } else {
                    messageFn('error', costR.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },

    },
});
